#pragma once

#include <stdlib.h>
#include <sys/mman.h>

namespace hawking {
namespace indexlib {

struct SysAllocator {
    void* Allocate(size_t bytes) {
        return ::malloc(bytes);
    }

    void Deallocate(void* ptr, size_t bytes) {
        ::free(ptr);
    }
};

struct MmapAllocator{
    void* Allocate(size_t bytes) {
        void* ret = ::mmap(
            nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        return (ret == MAP_FAILED) ? nullptr : ret;
    }

    void Deallocate(void* ptr, size_t bytes) {
        munmap(ptr, bytes);
    }
};


}
}
